﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.SessionState;
using Discuz.Toolkit;
using System.Web;

namespace Icst.Leaflike
{
    public static class SessionExtension
    {
        public static void setVersion(this HttpSessionState session, Version version, int expires, string cookieDomain)
        {
            HttpCookie cookie = new HttpCookie("alver");
            cookie.Values["version"] = version.ToString();
            cookie.Values["referer"] = "index.htm";
            cookie.Values["expires"] = expires.ToString();
            if (expires > 0)
            {
                cookie.Expires = DateTime.Now.AddMinutes(expires);
            }
            cookie.Domain = cookieDomain;
            HttpContext.Current.Response.AppendCookie(cookie);
        }

        public static Version getVersion(this HttpSessionState session,string cookieDomain)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies.Get("alver");
            if (cookie != null)
            {
                cookie.Domain = cookieDomain;
                string version = cookie["version"];
                return string.IsNullOrEmpty(version) ? Version.Public : (Version)Enum.Parse(typeof(Version), version, true);
            }
            else
                return Version.Public;
        }
    }
}
